package woodnaonly.radiobutto_test.Adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

import me.nereo.multi_image_selector.bean.Image;
import woodnaonly.radiobutto_test.R;

/**
 * Created by Administrator on 2015/11/4.
 */
public class GridAdapter extends BaseAdapter
{
    ArrayList<String> mSelectPath;
    Context context;
    private GridView.LayoutParams mItemLayoutParams;
    private int mItemSize;

    public GridAdapter(Context context, ArrayList<String> mSelectPath)
    {
        super();
        this.mSelectPath = mSelectPath;
        this.context = context;
        mItemLayoutParams = new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.MATCH_PARENT);
    }

    public void setItemSize(int columnWidth)
    {
        mItemSize = columnWidth;
        mItemLayoutParams = new GridView.LayoutParams(columnWidth, columnWidth);
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return mSelectPath.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mSelectPath.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        VHolder mVHolder;
        if (convertView == null)
        {
            mVHolder = new VHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.layout1_grid_item, null);

            convertView.setTag(mVHolder);
        } else
        {
            mVHolder = (VHolder) convertView.getTag();
        }

        mVHolder.imageview = (ImageView) convertView;
        mVHolder.bindData(mSelectPath.get(position));
        convertView.setLayoutParams(mItemLayoutParams);
        return convertView;
    }

    public class VHolder
    {
        ImageView imageview;

        void bindData(final String path)
        {

            File imageFile = new File(path);


            // 显示图片
            Picasso.with(context)
                    .load(imageFile)
                    .placeholder(me.nereo.multi_image_selector.R.drawable.default_error)
                            //.error(R.drawable.default_error)
                    .resize(mItemSize, mItemSize)
                    .centerCrop()
                    .into(imageview);
        }
    }



}
